package com.wangdao.wechatmall.realm;

import com.wangdao.wechatmall.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ZhangXiao on 2021/7/12
 */
@Component
public class WXRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;

    //处理认证信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        List<String> strings = userMapper.selectPassword(username);
        String credential = strings.size() >= 1 ? strings.get(0) : null;
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, credential, ByteSource.Util.bytes(username),this.getName());


        /**
         * 加密后的密文查看
         * "MD5" → 加密算法
         * token.getPassword() → 登录时输入的密码
         * username → 加盐
         * 3 → 迭代次数
         */

        SimpleHash md5 = new SimpleHash("MD5", token.getPassword(), username, 3);
        System.out.println(md5);


        return simpleAuthenticationInfo;
    }

    //处理授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


}
